package org.video.cj.lmp;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.video.cj.cjinfodao;
import org.video.cj.info.dsjlistinfo;
import org.video.cj.info.dyListinfo;
import org.video.cj.info.indexinfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class cjinfolmp implements cjinfodao {

	@Override
	public List<indexinfo> index(String path) {
		// path="https://www.360kan.com/dianying/list.php?year=all&area=all&act=all&cat=all&pageno=1";
		String names;//视频名
		String jieshaos;//简介
		String imgs;//视频图片
		String time;//视频更新集数
		String star;//视频更新集数
		String hrefs;//二级链接
		String fenlei = null;
		List<indexinfo> index=new ArrayList<indexinfo>() ;
		
		try {
			
			Document doc = Jsoup.connect(path)
					.header("User-Agent",
							"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
					.timeout(20000).get();
			Elements content=doc.getElementsByClass("list").select("li");
			int size=doc.getElementsByClass("list").select("li").size();
			System.out.println(size);
			for (Element element : content) {
				//System.out.println(element.html());
					 names=element.getElementsByClass("s1").text();
					 //System.out.println("名字"+names);
					 jieshaos=element.getElementsByClass("star").text();
					// System.out.println("介绍"+jieshaos);
					 imgs=element.getElementsByTag("img").attr("src");
					// System.out.println("图片"+imgs);
					 time=element.getElementsByClass("hint").text();
					// System.out.println("时间"+time);
					 if (element.getElementsByClass("point") != null) {
						 star=element.getElementsByClass("point").text();
						// System.out.println("星"+star);
					}else {
						star=null;
					}
					
					 hrefs=element.getElementsByClass("js-tongjic").attr("href");
					// System.out.println("链接"+hrefs);
//					System.out.println("名字"+names);
//					 System.out.println("介绍"+jieshaos);
//					System.out.println("图片"+imgs);
//					System.out.println("集数"+jis);
//					System.out.println("链接"+hrefs);
					
					index.add(new indexinfo(names, jieshaos, imgs, time,star, "https://www.360kan.com"+hrefs));
			}	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return index;
	}

	@Override
	public List<dyListinfo> dy(String path) {
		List<dyListinfo> dy=new ArrayList<dyListinfo>() ;
		try {
			Document doc = Jsoup.connect(path)
					.header("User-Agent",
							"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
					.timeout(20000).get();
			Elements content=doc.getElementsByClass("top");
			
			for (Element element : content) {
				String name=element.getElementsByTag("h1").text();
				System.out.println(name);
				String text=element.getElementsByClass("item-desc").text();
				text=text.substring(0, text.length()-4);
				System.out.println(text);

				String video=element.getElementsByClass("btn-play").attr("href");
				System.out.println(video);
				dy.add(new dyListinfo(name, text, video));
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		return dy;
	}

	
	@Override
	public List<dsjlistinfo> dsj(String path) {
		List<Integer> numt = new ArrayList<Integer>();
		List<String> video = new ArrayList<String>();
		List<dsjlistinfo> list=new ArrayList<dsjlistinfo>() ;
		try {
			Document doc = Jsoup.connect(path)
					.header("User-Agent",
							"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
					.timeout(20000).get();
			Elements content=doc.getElementsByClass("s-top-right");
			for (Element element : content) {
				String h1=element.getElementsByTag("h1").text();
				System.out.println(h1);
				String text=element.getElementsByClass("item-desc").text();
				System.out.println(text);
				Elements num=element.getElementsByClass("num-tab");
				int n=element.getElementsByClass("num-tab").size();
				for (Element element2 : num) {
					int ns=element2.children().size();
					if(ns<2){
					Elements nums=element2.child(0).children();
					int s=element2.child(0).children().size();
					nums.remove(s-1);
					for (Element element3 : nums) {
						
						String href=element3.attr("href");
						String data=element3.attr("data-num");
						System.out.println("网址"+href);
						System.out.println("数字"+data);
						//numt.add(Integer.parseInt(data));
						video.add(href);
					}

				}else {
						Elements nums=element2.child(1).children();
						int s=element2.child(1).children().size();
						nums.remove(s-1);
						for (Element element3 : nums) {

							String href=element3.attr("href");
							String data=element3.attr("data-num");
							System.out.println("网址"+href);
							System.out.println("数字"+data);
							//numt.add(Integer.parseInt(data));
							video.add(href);
						}

					}
				}
				list.add(new dsjlistinfo(h1, text, numt, video));
				
			}
			
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public List<dsjlistinfo> dm(String path) {

		List<Integer> numt = new ArrayList<Integer>();
		List<String> video = new ArrayList<String>();
		List<dsjlistinfo> list=new ArrayList<dsjlistinfo>() ;
		try {
			Document doc = Jsoup.connect(path)
					.header("User-Agent",
							"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
					.timeout(20000).get();
			Elements content=doc.getElementsByClass("m-top-dongman");
			for (Element element : content) {
				String name=element.getElementsByTag("h1").text();
				String text=element.getElementsByClass("item-desc").text();
				Elements e=element.getElementsByClass("site-wrap");
				for (Element element2 : e) {
					
					if (element2.child(1).children().size()>2) {
						Elements el=element2.child(1).child(2).select("a");
						int s=element2.child(1).child(2).select("a").size();
						System.out.println(s+"级数");
						if (s>=50&s<100) {
							el.remove(50);
						}else if (s>=100) {
							el.remove(50);
							el.remove(100);
						}else if (s>=150) {
							el.remove(50);
							el.remove(100);
							el.remove(150);
						}
//						el.remove(s-1);
						for (Element element3 : el) {
							String data=element3.attr("data-num");
							
							String hrefs=element3.attr("href");
							System.out.println("序号"+data+"\nhrefs"+hrefs);
							
							numt.add(Integer.parseInt(data));
							video.add(hrefs);
						}
					}else {
						Elements el=element2.child(1).child(0).select("a");
						int s=element2.child(1).child(0).select("a").size();
						System.out.println(s+"级数");
						if (s>13&s<27) {
							el.remove(14);
							
						}else if (s>100) {
							el.remove(14);
							el.remove(27);
							el.remove(100);
						}
//						el.remove(s-1);
						for (Element element3 : el) {
						
							String data=element3.attr("data-num");
							
							String hrefs=element3.attr("href");
							System.out.println("序号"+data+"\nhrefs"+hrefs);
							
							numt.add(Integer.parseInt(data));
							video.add(hrefs);
						}
						
						
					}
					
					
				}
				
				list.add(new dsjlistinfo(name, text, numt, video));
				
			}
			
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		return list;
	}

	@Override
	public List<indexinfo> sou(String path) {
		String names;//视频名
		String jieshaos;//简介
		String imgs;//视频图片
		String time;//视频更新集数
		String star;//视频打分
		String hrefs;//二级链接
		String fenlei;
		List<indexinfo> index=new ArrayList<indexinfo>() ;
		try {
			
			Document doc = Jsoup.connect(path)
					.header("User-Agent",
							"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
					.timeout(20000).get();
			
			Elements content=doc.getElementsByClass("js-longitem");
			for (Element element : content) {
				//System.out.println(element.html());
				
				
					imgs=element.child(0).child(0).child(0).attr("src");
					names=element.child(0).child(0).child(0).attr("alt");
					jieshaos=element.child(1).child(1).child(1).text();
					hrefs=element.child(0).child(0).attr("href");
					index.add(new indexinfo(names, jieshaos, imgs, null, null, hrefs));
					
					System.out.println(names);
					System.out.println(jieshaos);
					System.out.println(imgs);
					System.out.println(hrefs);
				}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return index;
	}

}
